package com.javacoo.fastdfs.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javacoo.fastdfs.client.api.client.FastDFSClient;
import com.javacoo.fastdfs.client.api.client.FastDFSClientFactory;
import com.javacoo.fastdfs.client.api.template.FastDFSTemplate;
import com.javacoo.fastdfs.client.api.template.FastDFSTemplateFactory;
import com.javacoo.fastdfs.client.config.FastDFSConfig;


/**
 * 自动配置类
 * <p>说明:</p>
 * <li></li>
 * @author duanyong@jccfc.com
 * @date  2020-05-02 15:30
 */
@Configuration                                                                                                                              
@EnableConfigurationProperties(value = FastDFSConfig.class)
@ConditionalOnClass(FastDFSClient.class)
@ConditionalOnProperty(prefix = FastDFSConfig.PREFIX, value = FastDFSConfig.ENABLED, matchIfMissing = true)
public class FastDFSAutoConfiguration {
	@Autowired                                                                                                                              
    private FastDFSConfig fastDFSConfig;
	
	@Bean
	@ConditionalOnMissingBean(FastDFSClient.class)
	public FastDFSClient createFastDFSClient() throws Exception {
		FastDFSTemplate fastDFSTemplate = FastDFSTemplateFactory.create().init(fastDFSConfig);
		return FastDFSClientFactory.create().init(fastDFSTemplate);
	}
}
